package action.admin;

import bdd.BddClientSelectRequest;
import bean.client.Client;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import service.securite.DroitsAcces;


/**
 * Servlet implementation class AdminGestionCompteAdmin
 */
public class AdminGestionCompteClientAction extends Action {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminGestionCompteClientAction() {
        super();
    }

    public static boolean aLesDroits(HttpServletRequest request) {
    	System.out.println("je verifie bien mes droits");
    	return (DroitsAcces.isAdminCompte(request));
    }
    
	protected void initSession(HttpServletRequest request )
	{
		HttpSession session = request.getSession();
	}	
	
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest pRequete,
			final HttpServletResponse pReponse) {
		ActionErrors errors = new ActionErrors();
		this.initSession(pRequete);
		
		if (!aLesDroits(pRequete)) {
			errors.add("errors",new ActionMessage("errors.admin"));
			saveErrors(pRequete,errors); 
			return (new ActionForward(pMapping.getInput()));
		}
		
		ArrayList<Client> listClient = null;
		String mail = "";
		String nomPrenom = "";
		String bloque = "";
		String min = "0";
		if ( pRequete.getParameter("min") != null )
			min = pRequete.getParameter("min");
		
		if ( pRequete.getParameter("action") != null && pRequete.getParameter("action").equals("recherche") ){
			mail = pRequete.getParameter("mail");
			nomPrenom = pRequete.getParameter("nomPrenom");
			bloque = pRequete.getParameter("bloque");
			
			try {
				listClient = BddClientSelectRequest.getClientsRecherche(mail, nomPrenom, false, bloque);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				errors.add("errors",new ActionMessage("errors.exception"));
				saveErrors(pRequete,errors); 
				return (new ActionForward(pMapping.getInput()));
			}			
		} else
			try {
				listClient = BddClientSelectRequest.getComptesClient(false);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				errors.add("errors",new ActionMessage("errors.exception"));
				saveErrors(pRequete,errors); 
				return (new ActionForward(pMapping.getInput()));
			}
		
		pRequete.setAttribute("listClients", listClient);
		pRequete.setAttribute("typeCompte", "clients");
		pRequete.setAttribute("mail", mail);
		pRequete.setAttribute("nomPrenom", nomPrenom);
		pRequete.setAttribute("bloque", bloque);
		pRequete.setAttribute("min", min);
		
		return pMapping.findForward("success");
	}
		

}
